package com.iot.websocket;

import com.iot.common.core.domain.entity.SysDept;
import com.iot.common.utils.spring.SpringUtils;
import com.iot.device.utils.SysDeptUtils;
import com.iot.websocket.message.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Cao.ning
 * @date 2022/6/27
 */
public class WebSocketMessageSender {

    private static final Logger logger = LoggerFactory.getLogger(WebSocketMessageSender.class);

    public static void send(WebSocketMessage message){
        final Set<String> targetSessionIds = new HashSet<>();
        String topic = message.getTopic();
        switch (topic) {
            case Topic.QUAKE_EVENT_MSG:
                // 事件信息
                EventMsgMessage clientStatusMessage_1 = (EventMsgMessage) message;
                SysDept sysDept_1 = SysDeptUtils.getSysDept(clientStatusMessage_1.getDeptId());
                String key_1 = sysDept_1.getAncestors().replace(",", "/");
                targetSessionIds.addAll(WebSocketManager.geQuakeEventMsgSubSessionIds(key_1));
                break;
            case Topic.GAS_EVENT_MSG:
                // 事件信息
                EventMsgMessage clientStatusMessage_2 = (EventMsgMessage) message;
                SysDept sysDept_2 = SysDeptUtils.getSysDept(clientStatusMessage_2.getDeptId());
                String key_2 = sysDept_2.getAncestors().replace(",", "/");
                targetSessionIds.addAll(WebSocketManager.getGasEventMsgSubSessionIds(key_2));
                break;
            case Topic.PS_EVENT_MSG:
                // 事件信息
                EventMsgMessage clientStatusMessage_3 = (EventMsgMessage) message;
                SysDept sysDept_3 = SysDeptUtils.getSysDept(clientStatusMessage_3.getDeptId());
                String key_3 = sysDept_3.getAncestors().replace(",", "/");
                targetSessionIds.addAll(WebSocketManager.getPressureSensorEventMsgSubSessionIds(key_3));
                break;
            case Topic.BEIDOU_EVENT_MSG:
                // 事件信息
                EventMsgMessage clientStatusMessage_4 = (EventMsgMessage) message;
                SysDept sysDept_4 = SysDeptUtils.getSysDept(clientStatusMessage_4.getDeptId());
                String key_4 = sysDept_4.getAncestors().replace(",", "/") + "/" + sysDept_4.getDeptId();
                targetSessionIds.addAll(WebSocketManager.getBeidouEventMsgSubSessionIds(key_4));
                break;
            case Topic.GAS_VBAT:
                // 电池电压报警
                VbatMessage vbatMessage = (VbatMessage) message;
                targetSessionIds.addAll(WebSocketManager.getTerminalResultSubSessionIds(vbatMessage.getClientNo()));
                break;
            case Topic.GAS_GPS:
                // 设备定位信息
                GpsMessage gpsMessage = (GpsMessage) message;
                targetSessionIds.addAll(WebSocketManager.getConfigChangeSubSessionIds(gpsMessage.getClientNo()));
                break;
            default:
                try{
                    CommonMessage commonMessage = (CommonMessage) message;
                    targetSessionIds.addAll(WebSocketManager.getCommonSubSessionIds(commonMessage.getTopic(), commonMessage.getClientNo()));
                }catch (Exception e){
                    // logger
                    logger.error("websocket消息发送CommonMessage转换异常，errmsg：{}", e.getMessage());
                }
                break;
        }
        send(targetSessionIds, message);
    }

    public static void send(Set<String> targetSessionIds, WebSocketMessage message){
        ThreadPoolTaskExecutor threadPoolTaskExecutor = SpringUtils.getBean("threadPoolTaskExecutor");
        threadPoolTaskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                WebSocketManager.sendMessage(targetSessionIds, message);
            }
        });
    }

    public static void sendEew(String message) {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = SpringUtils.getBean("threadPoolTaskExecutor");
        threadPoolTaskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                WebSocketManager.sendAll(message);
            }
        });
    }
}
